package com.bltec.emergency.controller;

import com.alibaba.fastjson.JSON;
import com.bltec.emergency.common.Constant;
import com.bltec.emergency.common.ResponseJson;
import com.bltec.emergency.model.Laws;
import com.bltec.emergency.service.mgrService.UserService;
import com.bltec.emergency.service.lawsService.LawsService;
import com.bltec.emergency.utils.DateUtil;
import com.bltec.emergency.utils.FileUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/laws")
@RequiresPermissions("project:laws")
public class LawsController {
    @Resource
    private LawsService lawsService;
    @Resource
    private UserService userService;

    @RequestMapping("getAllLaws")
    public ResponseJson getAllLaws() {
        try {
            List<Laws> laws = lawsService.getAll();
            return new ResponseJson(Constant.OK, JSON.toJSON(laws));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseJson(Constant.FAIL, e.getMessage());
        }
    }

    /*      private String lawsAndRegulations;
    private String standardSpecification;
    private String fileName;
    private String fileHref;*/
    @RequiresPermissions("laws:insert")
    @RequestMapping("addLaws")
    public ResponseJson addLaws(String lawsAndRegulations, String standardSpecification, String fileName, @RequestParam(value = "fileHref",required = false) MultipartFile fileHref ) {
        try {
            String filePath= FileUtils.writeToFile(fileHref.getBytes(), ResourceUtils.getURL("classpath:").getPath(),fileHref.getOriginalFilename());
            Laws laws = new Laws();
            laws.setLawsAndRegulations(lawsAndRegulations);
            laws.setStandardSpecification(standardSpecification);
            laws.setFileName(fileName);
            laws.setFileHref(filePath);
            laws.setEnable(true);
            laws.setCreateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            laws.setCreateTime(DateUtil.dateNow());
            lawsService.add(laws);
            return new ResponseJson(Constant.OK, "创建用户成功");
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, "创建用户失败" + e.getMessage());
        }
    }

    @RequiresPermissions("laws:update")
    @RequestMapping("updateLaws")
    public ResponseJson updateLaws(Long id,String lawsAndRegulations, String standardSpecification, String fileName, @RequestParam(value = "fileHref",required = false) MultipartFile fileHref) {
        try {
            String filePath= FileUtils.writeToFile(fileHref.getBytes(), ResourceUtils.getURL("classpath:").getPath(),fileHref.getOriginalFilename());
            Laws laws = lawsService.getLawsById(id);
            laws.setLawsAndRegulations(lawsAndRegulations);
            laws.setStandardSpecification(standardSpecification);
            laws.setFileName(fileName);
            laws.setFileHref(filePath);
            laws.setEnable(true);
            laws.setUpdateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            laws.setUpdateTime(DateUtil.dateNow());
            lawsService.updateLawsById(laws);
            return new ResponseJson(Constant.OK, JSON.toJSON(laws));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseJson.UPDATE_ERROR;
        }
    }
    @RequiresPermissions("laws:delete")
    @RequestMapping("deleteLaws")
    public ResponseJson setDisable(Long id) {
        if (null != lawsService.getLawsById(id)) {
            if (lawsService.deleteLawsById(id)) {
                return new ResponseJson(Constant.OK, "删除成功");
            } else {
                return new ResponseJson(Constant.FAIL, "删除失败");
            }
        } else {
            return new ResponseJson(Constant.OK, "删除成功");
        }
    }
}
